package com.wangshuo.common;

import com.wangshuo.domain.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    public static List<SysMenu> buildTree(List<SysMenu> list) {
    List<SysMenu> trees=new ArrayList<>();
    for (SysMenu sysMenu:list){
        if(sysMenu.getParentId().longValue() == 0){
            trees.add(findChildren(sysMenu,list));
        }
    }
    return trees;
    }

    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> list) {
    sysMenu.setChildren(new ArrayList<SysMenu>());
    for (SysMenu it:list){
        if(sysMenu.getId().longValue() == it.getParentId().longValue()){
            if(sysMenu.getChildren() == null){
                sysMenu.setChildren(new ArrayList<>());

            }
            sysMenu.getChildren().add(findChildren(it,list));
        }
    }
    return sysMenu;
    }
}
